package net.sapfree.core.db;

import javax.servlet.http.HttpServletRequest;

import net.sapfree.core.web.URLProcessor;

public class PagingInfo
{
	private int m_iPageSize ;
	private int m_iCurrerntPageNo ;
	private int m_iTotalPageCount ;
	private int m_iTotalRecordCount ;
	private String m_sEncodingURL ;
	private static URLProcessor m_processor = new URLProcessor();
	
	public boolean isNeedCountTotal()
	{
		return true;
	}
	
	public boolean isFirstPage()
	{
		return m_iCurrerntPageNo <= 1;
	}
	
	public boolean isLastPage()
	{
		return m_iCurrerntPageNo >= m_iTotalPageCount;
	}
	
	public PagingInfo(){}
	
	public PagingInfo(HttpServletRequest request, int iPageSize, String sRegion)
	{
		String pageno = request.getParameter("pageno");
		String pagesize = request.getParameter("pagesize");
		m_iCurrerntPageNo = (pageno == null || "".equals(pageno)) ? 1 : Integer.parseInt(pageno);
		m_iPageSize = (pagesize == null || "".equals(pagesize)) ? iPageSize : Integer.parseInt(pagesize);
		m_sEncodingURL = m_processor.encode(request, sRegion);
	}
	
	public String getEncodingURL()
	{
		return m_sEncodingURL;
	}
	
	public void setCurrentPageNo(int iPageNo)
	{
		m_iCurrerntPageNo = iPageNo;
	}
	
	public int getCurrentPageNo()
	{
		return m_iCurrerntPageNo;
	}
	
	public void setPageSize(int iPageSize)
	{
		m_iPageSize = iPageSize;
	}
	
	public int getPageSize()
	{
		return m_iPageSize;
	}
	
	public int getTotalPageCount()
	{
		return m_iTotalPageCount;
	}
	
	public void setTotalRecordCount(int iRecordCount)
	{
		m_iTotalRecordCount = iRecordCount;
		m_iTotalPageCount = iRecordCount / m_iPageSize ;
		if(m_iTotalPageCount * m_iPageSize < iRecordCount) 
		{
			m_iTotalPageCount ++;
		}
	}
	
	public int getTotalRecordCount()
	{
		return m_iTotalRecordCount;
	}
	
	public String getJsonString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("'pagesize':").append(m_iPageSize).append(",");
		sb.append("'pageno':").append(m_iCurrerntPageNo).append(",");
		sb.append("'pagecount':").append(m_iTotalPageCount).append(",");
		sb.append("'recordcount':").append(m_iTotalRecordCount);
		sb.append("}");
		return sb.toString();
	}
}
